package p532.p1.implementations;

import acm.graphics.GLabel;
import acm.graphics.GObject;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import java.util.Timer;
import p532.p1.interfaces.abGameBoard;
import p532.p1.interfaces.iGameObject;

/**
 *
 * @author han
 */
public class GameBoard extends abGameBoard implements MouseListener {

    //private GLabel textDisplay, status;
    private int width, height, life, score;
    private Timer timer;

    @Override
    public void startGame() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void initialize() {
        //get parameters from html
        width = getWidth();
        height = getHeight();
        getGCanvas().setSize(width, height);
        getGCanvas().setBackground(Color.gray);
    }

    @Override
    public void initGame() {
        //TODO need add more bricks or other things

        Paddle paddleObject = new Paddle(width / 2, height - 50, 60, 10, 8, Color.BLACK);
        setGamePaddle(paddleObject);

	    Brick brickObject = new Brick(20, 20 ,30,30, Color.RED);
        //setGameBrick(brickObject);


        Ball ballObject = new Ball(1, 1, 15, 15, Color.BLACK);
        setGameBall(ballObject);
        addGameObjects(new iGameObject[]{paddleObject, ballObject,brickObject});//can have more game objects

        addMouseListeners();
        setCurrentGameState(GAME_STATE_STARTED);

        timer = new Timer();
        timer.schedule(new GameExecution(this), new Date(), GAME_FRAME_FREQUENCY);
    }

    @Override
    public void run() {
        setCurrentGameState(GAME_STATE_PREPARE);

        initialize();
        initGame();
    }

    @Override
    public void pauseGame() {
        timer.cancel();
    }

    @Override
    public void resumeGame() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public synchronized void addGameObjects(iGameObject[] objects) {
        for (iGameObject o : objects) {
            add((GObject) o);
        }
    }

    @Override
    public void displayInfo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void displayStatus() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public synchronized void removeGameObject(iGameObject object) {
        remove((GObject) object);
    }

    @Override
    public void clearGame() {
        getGCanvas().removeAll();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if (getCurrentGameState() == GAME_STATE_STARTED) {
            Point movePos = e.getPoint();
            getGamePaddle().setLocation(movePos.x - (getGamePaddle().getWidth() / 2), getGamePaddle().getY());
        }
    }

    @Override
    public void checkCollision() {
        //TODO check if the ball collision with brick or wall, and then do the logic
    	
    }

    @Override
    public Boolean moveAllObjects() {
        //TODO move every object that should move
    	return getGameBall().moveToNextStepOfPath(width,height,4,3);
        	
		
        
    }
}
